#include "tools.h"
#include "../geometry/polygon.h"
#include "paintdata.h"
#include <iostream>
#include <fstream>
#include <string>
#include <QPushButton>
#include <QComboBox>
#include <QSpinBox>
#include <QWidget>
#include <QLayout>
#include <QLineEdit>
#include <cstdio>
#include <qt4/QtCore/qstring.h>

ToolsPanel::ToolsPanel(paint_data_t * data, QWidget * parent) : QWidget(parent), data_(data)
{
    QObject::connect(data_, SIGNAL(updated()), this, SLOT(recount_intersections()));

    setFixedSize(200, 600);
    QPushButton * save_button = new QPushButton("Save", this);
    save_button->setObjectName(QString::fromUtf8("save_button"));
    
    QPushButton * add_contour = new QPushButton("Add new contour");
    add_contour->setObjectName(QString::fromUtf8("add_contour"));

    list_ = new QSpinBox();
    list_->setMaximum(data_->polygon().size() - 1);
    list_->setObjectName(QString::fromUtf8("list"));

    QPushButton * delete_contour = new QPushButton("Delete contour");
    delete_contour->setObjectName(QString::fromUtf8("delete_contour"));

    intersections_ = new QLabel();

    cell_size_ = new QDoubleSpinBox();
    cell_size_->setSingleStep(.1);
    cell_size_->setValue(data->cell_size());
    cell_size_->setObjectName(QString::fromUtf8("cell_size"));
    
    QVBoxLayout * layout = new QVBoxLayout();
    setLayout(layout);
    layout->addWidget(save_button);
    layout->addWidget(add_contour);
    layout->addWidget(list_);
    layout->addWidget(delete_contour);
    layout->addWidget(cell_size_);
    layout->addWidget(intersections_);
    QMetaObject::connectSlotsByName(this);

}

void ToolsPanel::on_save_button_clicked()
{
    data_->save();
}

void ToolsPanel::on_add_contour_clicked()
{
    data_->add_new_contour();
    list_->setMaximum(data_->polygon().size() - 1);
    list_->setValue(data_->current_contour());
}

void ToolsPanel::on_list_valueChanged(int current_contour)
{
    data_->current_contour(current_contour);
}

void ToolsPanel::on_delete_contour_clicked()
{
    data_->remove(data_->current_contour());
    list_->setMaximum(data_->polygon().size() - 1);
}

void ToolsPanel::recount_intersections()
{
    if (data_->point_is_in())
    {
        intersections_->setText("Point is in");
    }
    else
    {
        intersections_->setText("Point is out");
    }
}

void ToolsPanel::on_cell_size_valueChanged(double value)
{
    data_->cell_size(value);
}
